package com.mouse.remind;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;


import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;


/**
 * 提醒（闹钟）实体类
 * @version 1.0
 * @created  
 */
public class AlarmModel extends Entity{
	
	private static final long serialVersionUID = 919251508183009690L;
	/*public enum Repeatday{
		ONLY_ONECE,
		ONECE_OF_DAY,
		ONECE_OF_WEEK,
		ONECE_OF_MONTH,
		ONECE_OF_YEAR;
	}*/
	public class Repeatday{
		public static final String ONLY_ONECE = "0";
		public static final String ONECE_OF_DAY = "1";
		public static final String ONECE_OF_WEEK = "2";
		public static final String ONECE_OF_MONTH = "3";
		public static final String ONECE_OF_YEAR = "4";
	}
	
	private int id;
	private String alarmid = "";         //提醒主键id
	private Boolean active = true;
	private Boolean vibrate = true;
	private Calendar alarmTime = Calendar.getInstance();
	
	private String subject = "";    //提醒内容
	private String year = ""+alarmTime.get(Calendar.YEAR);       //年
	private String month = ""+alarmTime.get(Calendar.MONTH);     //月
	private String day = ""+alarmTime.get(Calendar.DAY_OF_MONTH);       //日
	private String wday = ""+(alarmTime.get(Calendar.DAY_OF_WEEK)-1);       //周几      
	private String time = "";       //提醒时间
	private String uid = "";        //提醒人的uid
	private String touids = "";
	private String repeatday = "0";  //重复方式
	private String idtype = "";     //birth生日提醒
	private ArrayList<User> userList = new ArrayList<User>(); //提醒人
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getAlarmid() {
		return alarmid;
	}
	public void setAlarmid(String alarmid) {
		this.alarmid = alarmid;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getYear() {
		return year;
	}
	public void setYear(String year) {
		this.year = year;
	}
	public String getMonth() {
		return month;
	}
	public void setMonth(String month) {
		this.month = month;
	}
	public String getDay() {
		return day;
	}
	public void setDay(String day) {
		this.day = day;
	}
	public String getWday() {
		return wday;
	}
	public void setWday(String wday) {
		this.wday = wday;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
		setAlarmTime(time);
	}
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
	public String getTouids() {
		return touids;
	}
	public void setTouids(String touids) {
		this.touids = touids;
	}
	public String getRepeatday() {
		return repeatday;
	}
	public void setRepeatday(String repeatday) {
		this.repeatday = repeatday;
	}
	public String getIdtype() {
		return idtype;
	}
	public void setIdtype(String idtype) {
		this.idtype = idtype;
	}
	public ArrayList<User> getUserList() {
		return userList;
	}
	public void setUserList(ArrayList<User> userList) {
		this.userList = userList;
	}
	
	public Boolean getActive() {
		getAlarmTime();
		return active;
	}
	public void setActive(Boolean active) {
		this.active = active;
	}
	public Boolean getVibrate() {
		return vibrate;
	}
	public void setVibrate(Boolean vibrate) {
		this.vibrate = vibrate;
	}
	
	public Calendar getAlarmTime() {
		if (alarmTime.before(Calendar.getInstance()))
			alarmTime.add(Calendar.DAY_OF_MONTH, 1);
		
		String repeatday = getRepeatday();
		if (repeatday.equals(AlarmModel.Repeatday.ONLY_ONECE)) {
			alarmTime.set(Calendar.YEAR, Integer.parseInt(getYear()));
			alarmTime.set(Calendar.MONTH, Integer.parseInt(getMonth())-1);
			alarmTime.set(Calendar.DAY_OF_MONTH, Integer.parseInt(getDay()));
		}else if (repeatday.equals(AlarmModel.Repeatday.ONECE_OF_DAY)) {
		}else if (repeatday.equals(AlarmModel.Repeatday.ONECE_OF_WEEK)) {
			while(!getWday().equals((alarmTime.get(Calendar.DAY_OF_WEEK)-1)+"")){
				alarmTime.add(Calendar.DAY_OF_MONTH, 1);			
			}
		}else if (repeatday.equals(AlarmModel.Repeatday.ONECE_OF_MONTH)) {
			while(!getDay().equals(alarmTime.get(Calendar.DAY_OF_MONTH)+"")){
				alarmTime.add(Calendar.DAY_OF_MONTH, 1);			
			}
		}else if (repeatday.equals(AlarmModel.Repeatday.ONECE_OF_YEAR)) {
			alarmTime.set(Calendar.MONTH, Integer.parseInt(getMonth())-1);
			alarmTime.set(Calendar.DAY_OF_MONTH, Integer.parseInt(getDay()));
			if (alarmTime.before(Calendar.getInstance())) {
				alarmTime.add(Calendar.YEAR, 1);
			}
		}
		if (alarmTime.before(Calendar.getInstance())) {
			setActive(false);
		}
		return alarmTime;
	}

	public String getAlarmTimeStr() {

		String time = "";
		if (alarmTime.get(Calendar.HOUR_OF_DAY) <= 9)
			time += "0";
		time += String.valueOf(alarmTime.get(Calendar.HOUR_OF_DAY));
		time += ":";

		if (alarmTime.get(Calendar.MINUTE) <= 9)
			time += "0";
		time += String.valueOf(alarmTime.get(Calendar.MINUTE));

		return time;
	}

	public void setAlarmTime(Calendar alarmTime) {
		this.alarmTime = alarmTime;
	}

	public void setAlarmTime(String alarmTime) {
		
		String[] timePieces = alarmTime.split(":");
		if (timePieces.length == 2) {
			Log.d(this.getClass().getSimpleName(),alarmTime);

			Calendar newAlarmTime = Calendar.getInstance();
			newAlarmTime.set(Calendar.HOUR_OF_DAY,
					Integer.parseInt(timePieces[0]));
			newAlarmTime.set(Calendar.MINUTE, Integer.parseInt(timePieces[1]));
			newAlarmTime.set(Calendar.SECOND, 0);
			setAlarmTime(newAlarmTime);
		}
	}

	public void schedule(Context context) {
		setActive(true);
		
		Intent mIntent = new Intent(context, AlarmAlertReceiver.class);
		mIntent.putExtra("alarm", this);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, mIntent,PendingIntent.FLAG_CANCEL_CURRENT);
		AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
		alarmManager.set(AlarmManager.RTC_WAKEUP, getAlarmTime().getTimeInMillis(), pendingIntent);					
	}
	
	public String getTimeUntilNextAlarmMessage(){
		Log.d(this.getClass().getSimpleName(),"getTimeUntilNextAlarmMessage"+getAlarmTimeStr());
		long timeDifference = getAlarmTime().getTimeInMillis() - System.currentTimeMillis();
		long days = timeDifference / (1000 * 60 * 60 * 24);
		long hours = timeDifference / (1000 * 60 * 60) - (days * 24);
		long minutes = timeDifference / (1000 * 60) - (days * 24 * 60) - (hours * 60);
		long seconds = timeDifference / (1000) - (days * 24 * 60 * 60) - (hours * 60 * 60) - (minutes * 60);
		String alert = "亲！您成功设置了一个";
		if (days > 0) {
			alert += String.format(
					"%d 天, %d 小时, %d 分, %d 秒", days,
					hours, minutes, seconds);
		} else {
			if (hours > 0) {
				alert += String.format("%d 小时, %d 分, %d 秒",
						hours, minutes, seconds);
			} else {
				if (minutes > 0) {
					alert += String.format("%d  分, %d 秒", minutes,
							seconds);
				} else {
					alert += String.format("%d 秒", seconds);
				}
			}
		}
		alert += "后的提醒！";
		return alert;
	}
	
	public static void cancel(Context context) {
		Intent mIntent = new Intent(context, AlarmAlertReceiver.class);
		mIntent.putExtra("alarm", new AlarmModel());
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, mIntent,PendingIntent.FLAG_CANCEL_CURRENT);			
		AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
		
		alarmManager.cancel(pendingIntent);					
	}

	
}
